//
//  ShareView.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/4/16.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ShareView.h"
#import "WXApi.h"
#import "ProdcutDetaiModel.h"
#import "SDWebImageManager.h"
#import "JSONKit.h"
@implementation ShareView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ShareView" owner:self options:nil];
        UIView *view = views[0];
        view.frame = self.bounds;
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:view];
    }
    return self;
}

- (IBAction)closeAction:(id)sender {
    if (self.closeShareViewBlock) {
        self.closeShareViewBlock();
    }
}

- (IBAction)shareToFriends:(id)sender {
     NSDictionary *dir=[ShareView dictionaryWithJsonString:self.shareContent];
    if ([WXApi isWXAppInstalled]) {
        if (self.productModel) {
            SDWebImageManager *manager = [SDWebImageManager sharedManager];
            [manager downloadImageWithURL:[NSURL URLWithString:self.productModel.data.share_image] options:SDWebImageHighPriority progress:NULL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                WXWebpageObject *ext = [WXWebpageObject object];
                ext.webpageUrl = self.productModel.data.share_url;
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = self.productModel.data.share_title;
                message.description = self.productModel.data.share_text;
                message.mediaObject = ext;
                [message setThumbImage:[image imageCompressForWidth:45.f]];
                req.bText = NO;
                req.message = message;
                req.scene = WXSceneTimeline;
                [WXApi sendReq:req];
            }];
        }
    } else {
        if (self.shareContent.length>0&&self.shareContent) {
            NSDictionary *dir=[ShareView dictionaryWithJsonString:self.shareContent];
            SDWebImageManager *manager = [SDWebImageManager sharedManager];
            [manager downloadImageWithURL:[NSURL URLWithString:dir[@"iconUrl"]] options:SDWebImageHighPriority progress:NULL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                WXWebpageObject *ext = [WXWebpageObject object];
                ext.webpageUrl = dir[@"shareUrl"];
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = dir[@"title"];
                message.description = dir[@"content"];
                message.mediaObject = ext;
                [message setThumbImage:[image imageCompressForWidth:45.f]];
                req.bText = NO;
                req.message = message;
                req.scene = WXSceneTimeline;
                [WXApi sendReq:req];
            }];

            
        }
        
    }
}


- (IBAction)shareToFriend:(id)sender {
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    if (self.productModel) {
        [manager downloadImageWithURL:[NSURL URLWithString:self.productModel.data.share_image] options:SDWebImageHighPriority progress:NULL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
            WXWebpageObject *ext = [WXWebpageObject object];
            ext.webpageUrl = self.productModel.data.share_url;
            WXMediaMessage *message = [WXMediaMessage message];
            message.title = self.productModel.data.share_title;
            message.description = self.productModel.data.share_text;
            message.mediaObject = ext;
            [message setThumbImage:[image imageCompressForWidth:45.f]];
            req.bText = NO;
            req.message = message;
            req.scene = WXSceneSession;
            [WXApi sendReq:req];
        }];
    }else{
        if (self.shareContent.length>0&&self.shareContent){
            NSDictionary *dir=[ShareView dictionaryWithJsonString:self.shareContent];
            SDWebImageManager *manager = [SDWebImageManager sharedManager];
            [manager downloadImageWithURL:[NSURL URLWithString:dir[@"iconUrl"]] options:SDWebImageHighPriority progress:NULL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                WXWebpageObject *ext = [WXWebpageObject object];
                ext.webpageUrl = dir[@"shareUrl"];
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = dir[@"title"];
                message.description = dir[@"content"];
                message.mediaObject = ext;
                [message setThumbImage:[image imageCompressForWidth:45.f]];
                req.bText = NO;
                req.message = message;
                req.scene = WXSceneSession;
                [WXApi sendReq:req];
            }];

            
        }
    }
   
}
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

@end
